$(document).ready(function () {
	// VALIDATE LOGIN FORM
	var username = $("#loginform input[type='text']").val();
	var password = $("#loginform input[type='password']").val();
	
	$("#loginform input[type='text']").focus(function(){
		if(username == $(this).val()) {
			$(this).val("");
		}
	});
	$("#loginform input[type='text']").focusout(function(){
		if($(this).val() == "") {
			$(this).val(username);
		}
	});
	$("#loginform input[type='password']").focus(function(){
		if(password == $(this).val()) {
			$(this).val("");
		}
	});
	$("#loginform input[type='password']").focusout(function(){
		if($(this).val() == "") {
			$(this).val(password);
		}
	});
	
	$("#loginform input[type='text'], #loginform input[type='password']").keypress(function(event){
		 
		var keycode = (event.keyCode ? event.keyCode : event.which);
		if(keycode == '13'){
			var username = $("#loginform input[type='text']").val();
			var password = $("#loginform input[type='password']").val();
			
			if (username != "" && password != "") {
				var action = 'gig/ajax_login.php';
				var form_data = {
					ws_user: username,
					ws_pwd: password,
					is_ajax: 1
				};
				
				$.ajax({
					type: "POST",
					url: action,
					data: form_data,
					success: function(response)
					{	
						if(response == 'NO')
						alert("Invalid username and/or password");	
						else
							$("#loginform").slideUp('slow', function() {
								$("#header-right").css("display", "none");
								$("#loggedarea").html(response);
							});
					}
				});
			}
		}
		
		return false;
	});
	
	$('.popup-forgotpassword .button-close-popup').click(function() {
		resetForgotPasswordValue();
		$('.popup-openaccount, .popup-forgotpassword, .dialog3').css('display','none');
	});
	
	//Forgot password
	var rstEmail = $(".popup-forgotpassword input[type=text]").val();
	
	$(".popup-forgotpassword input[type=text]").focus(function(){
		if(rstEmail == $(this).val()) {
			$(this).val("");
		}
	});
	$(".popup-forgotpassword input[type=text]").focusout(function(){
		if($(this).val() == "") {
			$(this).val(rstEmail);
		}
	});
	
	nextStep1();
	
	$(".popup-forgotpassword form").submit(function () {
		
		var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
		
		var email = $(".popup-forgotpassword input[type=text]");
		
		if(!pattern.test(email.val())){
			email.parent().addClass("error");
			$(".errorMessage-forgotPassword span").html("Please provice your email!" + "<span></span>");
		} else {
			email.parent().removeClass("error");
		}
		if ($(".popup-forgotpassword label.error").length == 0) {
			var form_data = {					
					email: $(".popup-forgotpassword input[type=text]").val(),					
					type: "forget_email"
			};
			
			$.ajax({			
				type: "POST",			
				url: "ajax/ajax_forgot_password",			
				data: form_data,			
				dataType: "json",			
				success: function(response)			
				{				
					if (response.success == true) {
						nextStep2();
						//$('.popup-openaccount, .popup-forgotpassword, .dialog3').css('display','none');
					} else {
						email.parent().addClass("error");
						$(".errorMessage-forgotPassword span").html(response.message + "<span></span>");
					}
				}
			});	
		}
		
		return false;
	});
	
	
	$(".errorMessage-forgotPassword").click(function () {
		$(this).parent().removeClass("error");
	});
	
	function resetForgotPasswordValue() {
		$(".popup-forgotpassword label.error").removeClass("error");
		$(".popup-forgotpassword input[type=text]").val(rstEmail);
		nextStep1();
	}
	
	function nextStep1() {
		$(".popup-forgotpassword .state-1").css("display", "block");
		$(".popup-forgotpassword .state-2").css("display", "none");
		$(".popup-forgotpassword label.error").removeClass("error");
	}
	
	function nextStep2() {
		$(".popup-forgotpassword .state-1").css("display", "none");
		$(".popup-forgotpassword .state-2").css("display", "block");
		$(".popup-forgotpassword label.error").removeClass("error");
	}
});